//
//  ViewController.m
//  NewGongChwng
//
//  Created by lanou3g on 15/11/2.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技有限公司. All rights reserved.
//

#import "ViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "Student.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self createObject];
    [self createStudentObject];
//    [self updataStudemtObject];
//    [self selectStudent];
//    [self deleteObject];
}

- (void)createObject
{
    AVObject *teacher = [AVObject objectWithClassName:@"Teacher"];
    [teacher setObject:@"泽姐" forKey:@"name"];
    [teacher setObject:@"12" forKey:@"age"];
    [teacher setObject:[NSNumber numberWithInt:1] forKey:@"id"];
    // 直接传上去
//    [teacher save];
    // 子线程传上去
    [teacher saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"创建成功");
            NSLog(@"创建成功");
            NSLog(@"创建成功");
            NSLog(@"创建成功");
            
        }
    }];
}

- (void)createStudentObject
{
  
    Student *stu = [[Student alloc] init];
    stu.name = @"毛军";
    stu.age = @"18";
    stu.sex = @"男";
    stu.image = [AVFile fileWithName:@"100.ipg" contentsAtPath:[[NSBundle mainBundle] pathForResource:@"100" ofType:@"jpg"]];
    
    // 图片
    [stu saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"id= %@", stu.objectId);
        } else  {
            NSLog(@"错误信息%@", error);
            NSLog(@"错误信息jdsklj ");
            NSLog(@"错误信息%@", error);
             NSLog(@"错误信息%@", error);
             NSLog(@"错误信息%@", error);
              NSLog(@"错误信息%@", error);
              NSLog(@"错误信息%@", error);
        }
    }];
    
}

- (void)updataStudemtObject
{
    Student *stu = [Student objectWithoutDataWithObjectId:@""];
    stu.name = @"哈哈";
    [stu saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"更改成功");
        }
    }];
    
}

- (void)selectStudent
{
    Student *stu = [Student objectWithoutDataWithObjectId:@""];
    [stu fetchInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        if (!error) {
            NSString *name = [object valueForKey:@"name"];
            NSLog(@"name = %@", name);
        }
    }];
}

- (void)deleteObject
{
    Student *stu = [Student objectWithoutDataWithObjectId:@""];
    [stu deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"删除成功");
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
